﻿using System;
using System.Collections.Generic;
using System.Linq;
#if !NETCOREAPP
using System.Web;
using System.Web.Mvc;
using System.Web.WebPages;
    using System.Web.Mvc.Html;

#endif

#if NETCOREAPP
using Microsoft.AspNetCore.Mvc.Rendering;
using Kugar.Core.Web;
#endif
using Kugar.Core.ExtMethod;
using Kugar.MVC.MobileUI.WeUI.Helpers;

namespace Kugar.MVC.MobileUI.WeUI.PageControls.PageForm
{
    public class PageForm_MobileVCode:PageForm_FieldBase
    {
        private string _callUrl = "";
        private string _placeHolder = "请输入验证码";
        private string _value = "";
        private string _buttonScript = "";
        private string _forMobileCtrl = "";
        private string _paramName = "mobile";
        private int _disableSec = 30;
        private bool _isrequired;

        public PageForm_MobileVCode(ViewContext viewContext) : base(viewContext)
        {
            _attrs.Class("weui-cell_mobilevcode");
        }

        /// <summary>
        /// 点击获取验证码按钮需要直接调用的链接,需返回ResultReturn 格式的json
        /// </summary>
        /// <param name="callUrl"></param>
        /// <returns></returns>
        public PageForm_MobileVCode SMSCallUrl(string callUrl)
        {
            _callUrl = callUrl;

            return this;
        }

        /// <summary>
        /// 该参数,指定了,附带在SMSCallUrl链接?号后的参数的名称,默认为mobile
        /// </summary>
        /// <param name="argsName"></param>
        /// <returns></returns>
        public PageForm_MobileVCode QueryParamName(string argsName)
        {
            _paramName = argsName.ToStringEx().Trim();

            return this;
        }

        /// <summary>
        /// 当调用成功后,自动禁用按钮多少秒
        /// </summary>
        /// <param name="sec">禁用的秒数,单位为秒,默认为30</param>
        /// <returns></returns>
        public PageForm_MobileVCode DisableButtonForSec(int sec=30)
        {
            _disableSec = sec;

            return this;
        }

        public PageForm_MobileVCode Value(string value)
        {
            _value = value;

            return this;
        }



        public PageForm_MobileVCode Placeholder(string text)
        {
            _placeHolder = text;

            return this;
        }

        /// <summary>
        /// 指定手机验证码控件关联的手机号码所在控件
        /// </summary>
        /// <param name="selector">jquery的选择器,将使用在find中</param>
        /// <returns></returns>
        public PageForm_MobileVCode ForMobileTextBox(string selector)
        {
            _forMobileCtrl = selector;
            return this;
        }

        /// <summary>
        /// 用于指定按钮的点击事件,如果使用了本函数设定功能,则将替换掉默认的按钮功能
        /// </summary>
        /// <param name="script"></param>
        /// <returns></returns>
        public PageForm_MobileVCode OnButtonClick(string script)
        {
            _buttonScript = script;

            return this;
        }

        protected override void BeginRender()
        {
            base.BeginRender();

            this.Data("data-for", _forMobileCtrl.ToStringEx());
            this.Data("data-buttonscript", _buttonScript.ToStringEx());
            this.Data("data-paramname", _paramName);
            this.Data("data-smsurl", _callUrl.ToStringEx());
            this.Data("data-disablesec", _disableSec.ToStringEx());
        }

        protected override void RenderBody()
        {
            ViewContext.Writer.WriteLine($"<input class=\"weui-input\" type=\"text\" required placeholder=\"{_placeHolder}\" name=\"{_name}\" value=\"{_value}\" />");
        }

        protected override void RenderFooter()
        {
            base.RenderFooter();
            ViewContext.Writer.WriteLine($"<button class=\"weui-vcode-btn\" data-smsurl=\"{_callUrl}\">获取验证码</button>");
        }
    }
}